package com.hhkj.admin.course.common.component.checker;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hhkj.admin.course.domain.Course;
import com.hhkj.admin.course.service.base.CourseService;
import com.ruoyi.common.exception.GlobalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * CourseChecker
 *
 * @author HHKJ-SERVER-ZJ
 * @since 2023/6/13 15:38
 */
@Component
public class CourseChecker
{
    @Autowired
    private CourseService service;

    /**
     * 课程名称重复校验
     *
     * @param course 课程信息
     */
    public void checkCourseNameRepeat(Course course)
    {
        // 1、检测本级课程名称是否重复
        LambdaQueryWrapper<Course> courseQuery = new LambdaQueryWrapper<>();
        courseQuery.eq(Course::getName, course.getName())
                .eq(Course::getParentId, course.getParentId())
                .eq(ObjectUtil.isNotNull(course), Course::getId, course.getId());
        long repeatNameAmount = service.count(courseQuery);
        if (repeatNameAmount > 0L) {
            throw new GlobalException("课程信息重复");
        }
    }
}
